package com.hxd.mall.module.usercenter.fragment

import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectFragment
import com.hxd.mall.constant.HxdConstantsRouterPath.PathUserCenter.path_user_center_nick
import com.hxd.mall.module.usercenter.bean.Funsbean
import com.hxd.mall.module.usercenter.funs.FunsItemAdapter
import com.hxd.mall.module.usercenter.funs.IFunsManagerPresenter
import com.hxd.mall.module.usercenter.funs.IFunsManagerView
import com.hxd.mall.utils.local.NavigationUtil.routerNavigation
import com.hxd.mall.utils.open.addSetOnClickListener
import com.hxd.mall.utils.open.initRecyclerView
import kotlinx.android.synthetic.main.view_pager_funs_layout.*

/**
 * Create by yinzhengwei on 2019-11-05
 * @Function
 */
class FunsPageFragment : BaseInjectFragment<IFunsManagerPresenter>(), IFunsManagerView {

    private var pageNum = 1
    private var pageSize = 20
    private val adapter = FunsItemAdapter()
    private var moreView: View? = null

    override fun attachLayoutRes() = R.layout.view_pager_funs_layout

    override fun initViews() {
        //构造无更多数据的布局
        moreView = LayoutInflater.from(activity!!).inflate(R.layout.no_date_layout, null)

        funList.initRecyclerView(adapter, padding = 0, leftPadding = 0, color = R.color.color_EAEDF1)
        /*adapter.setOnItemClickListener { _, _, _ ->
            activity?.showFunInfoDialog("", "", "", "")
        }*/
        //构造空数据的view
        adapter.setEmptyView(R.layout.empty_fun_list_layout, funList)

        addSetOnClickListener(adapter.emptyView.findViewById<TextView>(R.id.share)) {
            routerNavigation(path_user_center_nick)
        }

        //在内容不满一页的时候，是否可以上拉加载更多
//        smart_refresh_layout.setEnableLoadMoreWhenContentNotFull(false)

        smart_refresh_layout.setOnRefreshListener {
            pageNum = 1
            initData()
        }
        smart_refresh_layout.setOnLoadMoreListener {
            pageNum++
            initData()
        }
        mPresenter?.getData(pageNum)
    }

    override fun initData() {
        mPresenter?.getData(pageNum)
    }

    override fun showData(t: Funsbean) {
        t?.data?.list?.run {
            if (this.isNotEmpty()) {
                if (pageNum == 1) {
                    adapter.setNewData(this)
                } else {
                    adapter.addData(this)
                }
            }
            adapter.removeAllFooterView()
            //结束刷新
            if (this.isEmpty() || this.size < pageSize) {
                if (pageNum != 1) adapter.addFooterView(moreView)
                smart_refresh_layout.setEnableLoadMore(false)
            } else {
                smart_refresh_layout.setEnableLoadMore(true)
            }
        }

        funList.background = if (adapter.data.isNullOrEmpty()) null else resources.getDrawable(R.drawable.shape_product_item_bg)
    }

    override fun finishRefresh() {
        smart_refresh_layout.finishRefresh()
        smart_refresh_layout.finishLoadMore()
    }

    override fun finishFailRefresh() {
        smart_refresh_layout.finishRefresh()
        adapter.loadMoreFail()
    }

}